#include "carcontrol.h"
#include <iostream>

CarControl::CarControl(HardInterface *hardInt, NetControl *netCtl, Ui::mainCar *ui)
{
    this->car = new Car();
    this->hardInt = hardInt;
    this->netCtl = netCtl;
    this->ui = ui;

    this->spurGefahr = new QImage();
    this->autobahn_1_1_1 = new QImage();
    this->autobahn_0_1_1 = new QImage();
    this->autobahn_0_1_2 = new QImage();
    this->autobahn_1_1_0 = new QImage();
    this->autobahn_2_1_0 = new QImage();
    this->autobahn_2_1_1 = new QImage();
    this->autobahn_1_1_2 = new QImage();
    this->autobahn_2_2_2 = new QImage();
    this->autobahn_1_2_1 = new QImage();
    this->autobahn_1_2_2 = new QImage();
    this->autobahn_2_2_1 = new QImage();
    this->autobahn_2_2_0 = new QImage();
    this->autobahn_1_2_0 = new QImage();
    this->autobahn_0_2_1 = new QImage();
    this->autobahn_0_2_2 = new QImage();
    this->autobahn_2_1_2 = new QImage();

    this->spurGefahr->load("Bilder/spurGefahr.jpg");

    if(this->autobahn_1_1_1->load("Bilder/autobahn_1_1_1.jpg"))
    {
        std::cout << "Bilder nicht geladen !" << std::endl;
    }

    this->autobahn_0_1_1->load("Bilder/autobahn_0_1_1.jpg");
    this->autobahn_0_1_2->load("Bilder/autobahn_0_1_2.jpg");
    this->autobahn_1_1_0->load("Bilder/autobahn_1_1_0.jpg");
    this->autobahn_2_1_0->load("Bilder/autobahn_2_1_0.jpg");
    this->autobahn_2_1_1->load("Bilder/autobahn_2_1_1.jpg");
    this->autobahn_1_1_2->load("Bilder/autobahn_1_1_2.jpg");
    this->autobahn_2_2_2->load("Bilder/autobahn_2_2_2.jpg");
    this->autobahn_1_2_1->load("Bilder/autobahn_1_2_1.jpg");
    this->autobahn_1_2_2->load("Bilder/autobahn_1_2_2.jpg");
    this->autobahn_2_2_1->load("Bilder/autobahn_2_2_1.jpg");
    this->autobahn_2_2_0->load("Bilder/autobahn_2_2_0.jpg");
    this->autobahn_1_2_0->load("Bilder/autobahn_1_2_0.jpg");
    this->autobahn_0_2_1->load("Bilder/autobahn_0_2_1.jpg");
    this->autobahn_0_2_2->load("Bilder/autobahn_0_2_2.jpg");
    this->autobahn_2_1_2->load("Bilder/autobahn_2_1_2.jpg");


    connect(this->hardInt,SIGNAL(initReady(int,int,int)),
            this,SLOT(initCar(int,int,int)));
    connect(this->hardInt,SIGNAL(aktualisiereCarData(QByteArray,QList<QByteArray>)),
            this, SLOT(aktualisiereCarData(QByteArray,QList<QByteArray>)));

    //connect(this->hardInt,SIGNAL(aktualisiereCarData(QByteArray)),
      //      this, SLOT(aktualisiereCarData(QByteArray)));
    //connect(this->hardInt,SIGNAL(aktualisiereCarsList(QList<QByteArray>)),
      //      this->netCtl,SLOT(aktualisiereCarsList(QList<QByteArray>)));
}

//destruktor
CarControl::~CarControl()
{
    delete car;
    delete spurGefahr;
    delete autobahn_1_1_1;
    delete autobahn_0_1_1;
    delete autobahn_0_1_2;
    delete autobahn_1_1_0;
    delete autobahn_2_1_0;
    delete autobahn_2_1_1;
    delete autobahn_1_1_2;
    delete autobahn_2_2_2;
    delete autobahn_1_2_1;
    delete autobahn_1_2_2;
    delete autobahn_2_2_1;
    delete autobahn_2_2_0;
    delete autobahn_1_2_0;
    delete autobahn_0_2_1;
    delete autobahn_0_2_2;
    delete autobahn_2_1_2;
}

void CarControl::aktualisiereCarData(QByteArray carDat, QList<QByteArray> listDaten)
{
   QList<QByteArray> datList = carDat.split('@');

   if(datList.size() != 5)
   {
    std::cout << "FEHLER beim Datenaktualisieren fuer dieses Auto!" << std::endl;
   }

   car->setId(datList.at(0).toInt());
   car->setSpeed(datList.at(1).toInt());
   car->setPosition(datList.at(3).toInt(),
                    datList.at(4).toInt());

   this->netCtl->aktualisiereCarsList(listDaten);
   this->sucheAutosInUmgebung();
   this->aktualisiereStatusBild();
   this->sendData();

   emit updateGui();

}
void CarControl::aktualisiereStatusBild()
{
    //auto status 1_1_0
    if(this->netCtl->dovorAuto.getId() == 0 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_0_2_1));
    }
    //auto status 2_1_0
    else if(this->netCtl->dovorAuto.getId() == 0 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_0_2_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
         ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_2_1));
    }
    else if(this->netCtl->dovorAuto.getId() == 0 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_0_1_1));
    }
    else if(this->netCtl->dovorAuto.getId() == 0 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_0_1_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getId() == 0 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_1_0));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getId() == 0 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_1_0));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getId() == 0 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_2_0));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getId() == 0 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_2_0));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_1_1));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_1_1));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_1_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 1 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_1_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_2_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 1 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 2 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_1_2_2));
    }
    else if(this->netCtl->dovorAuto.getSpur() == 2 &&
       this->car->getSpur() == 2 &&
       this->netCtl->danachAuto.getSpur() == 1 )
    {
        ui->label_17->setPixmap(QPixmap::fromImage(*this->autobahn_2_2_1));
    }
}

void CarControl::sucheAutosInUmgebung()
{
    Car tempCar;
    Car leerCar;
    bool keinerVor = true;
    bool keinerNach = true;

    this->netCtl->danachAuto = leerCar;
    this->netCtl->dovorAuto = leerCar;

    if(this->netCtl->autoDataList.size() == 0) return;
    if(this->netCtl->autoDataList.size() == 1)
    {
        tempCar = this->netCtl->autoDataList.at(0);
        if( tempCar.getWeite() > this->car->getWeite())
        {
            this->netCtl->dovorAuto = this->netCtl->autoDataList.at(0);
            keinerVor = false;
        }
        else
        {
            this->netCtl->danachAuto = this->netCtl->autoDataList.at(0);
            keinerNach = false;
        }
    }
    else
    {
        foreach(Car thisCar, this->netCtl->autoDataList)
        {
            if(thisCar.getWeite() > this->car->getWeite() &&
               (thisCar.getWeite() < this->netCtl->dovorAuto.getWeite() ||
                this->netCtl->dovorAuto.getWeite() == 1))
            {
                this->netCtl->dovorAuto = thisCar;
                keinerVor = false;

            }
            else if(thisCar.getWeite() < this->car->getWeite() &&
               thisCar.getWeite() >= this->netCtl->danachAuto.getWeite())
            {
                this->netCtl->danachAuto = thisCar;
                keinerNach = false;
            }
        }
    }
}

void CarControl::initCar(int id, int spur, int weite)
{
   car->setId(id);
   car->setPosition(spur, weite);
   emit updateGui();

   sendData();
}

void CarControl::sendData()
{
   QByteArray id;
   QByteArray panne;

   id.setNum(car->getId(), 10);
   panne.setNum(car->getPanne());
   id.append("@");
   id.append(panne);
   id.append("@");

   emit sendDataSig(id);
}
void CarControl::tryChangeSpeed(int newspeed)
{
    long abstand = 0;
    bool changeok = true;
    if(this->netCtl->autoDataList.isEmpty())
    {
        this->hardInt->geschwSoll = newspeed;
        std::cout << " CHANGE SPEED" << std::endl;
        changeok = false;
    }
    else
    {
        foreach(Car tempCar, this->netCtl->autoDataList)
        {
            if(tempCar.getSpur() == this->car->getSpur())
            {
                abstand = tempCar.getWeite() - this->car->getWeite();
                if(abstand > 0 && (this->car->getSpeed()/abstand) >= 2)
                {
                    changeok = false;
                    ui->spinBox_2->setValue(this->hardInt->geschwSoll);

                }
            }
        }
        if(changeok)
        {
            this->hardInt->geschwSoll = newspeed;
            std::cout << " CHANGE SPEED" << std::endl;
        }
    }
}

void CarControl::tryChangeSpur(int newspur)
{
    long abstand = 0;
    double faktor = 0;
    bool changeok = true;
    if(this->netCtl->autoDataList.isEmpty())
    {
        this->hardInt->spurSoll = newspur;
        std::cout << " CHANGE SPUR" << std::endl;
        changeok = false;
    }
    else
    {
        foreach(Car tempCar, this->netCtl->autoDataList)
        {
            if(tempCar.getSpur() != this->car->getSpur())
            {
                //hinter dem auto ?
                abstand = this->car->getWeite() - tempCar.getWeite();
                faktor = 2 - abstand/1000;
                if(abstand > 0 &&
                   faktor > 0 &&
                   (this->car->getSpeed()*faktor > tempCar.getSpeed()))
                {
                    changeok = false;
                    ui->spinBox_2->setValue(this->hardInt->spurSoll);
                    ui->label_16->setPixmap(QPixmap::fromImage(*this->spurGefahr));

                }
            }
        }
        if(changeok)
        {
            this->hardInt->spurSoll = newspur;
            std::cout << " CHANGE SPUR" << std::endl;
        }
    }

}



int CarControl::getCarId()
{
   return car->getId();
}

int CarControl::getCarSpur()
{
   return car->getSpur();
}

int CarControl::getCarSpeed()
{
   return car->getSpeed();
}

int CarControl::getCarWeite()
{
   return car->getWeite();
}



